<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修仙模拟器</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <!-- 配置Tailwind自定义颜色和字体 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#8B5CF6', // 紫色作为主色调，代表神秘和仙气
                        secondary: '#10B981', // 绿色作为辅助色，代表生机
                        accent: '#F59E0B', // 金色作为强调色，代表稀有和尊贵
                        dark: '#1E293B',
                        light: '#F8FAFC'
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                        serif: ['"Noto Serif SC"', 'serif']
                    }
                }
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .text-shadow {
                text-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .text-glow {
                text-shadow: 0 0 10px rgba(139, 92, 246, 0.6);
            }
            .bg-gradient-cultivation {
                background: linear-gradient(135deg, #0F172A 0%, #1E293B 100%);
            }
            .animate-float {
                animation: float 6s ease-in-out infinite;
            }
            @keyframes float {
                0% { transform: translateY(0px); }
                50% { transform: translateY(-10px); }
                100% { transform: translateY(0px); }
            }
            .animate-pulse-slow {
                animation: pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite;
            }
            
            /* 自定义滚动条样式 */
            .custom-scrollbar::-webkit-scrollbar {
                width: 6px;
            }
            .custom-scrollbar::-webkit-scrollbar-track {
                background: rgba(30, 41, 59, 0.5);
                border-radius: 10px;
            }
            .custom-scrollbar::-webkit-scrollbar-thumb {
                background: rgba(139, 92, 246, 0.5);
                border-radius: 10px;
            }
            .custom-scrollbar::-webkit-scrollbar-thumb:hover {
                background: rgba(139, 92, 246, 0.8);
            }
        }
    </style>
</head>
<body class="bg-gradient-cultivation text-light min-h-screen font-sans">
    <!-- 游戏容器 -->
    <div class="container mx-auto px-4 py-8 max-w-6xl">
        <!-- 开始界面 -->
        <div id="start-screen" class="flex flex-col items-center justify-center min-h-[70vh]">
            <h1 class="text-[clamp(2.5rem,6vw,4rem)] font-bold text-center mb-6 text-primary text-glow">修仙模拟器</h1>
            <p class="text-xl text-center mb-10 max-w-2xl text-gray-300">
                从一介凡人开始，踏上修仙之路。修炼、突破、渡劫，追求大道永恒。
                你的每一个选择都将影响最终的命运。
            </p>
            <div class="flex flex-col sm:flex-row gap-4">
                <button id="start-btn" class="px-8 py-3 bg-primary hover:bg-primary/80 text-white rounded-lg text-lg font-medium transition-all duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-primary/50">
                    <i class="fa fa-play mr-2"></i>开始修仙
                </button>
                <button id="help-btn" class="px-8 py-3 bg-dark hover:bg-dark/80 text-white rounded-lg text-lg font-medium transition-all duration-300 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-gray-500/50">
                    <i class="fa fa-question-circle mr-2"></i>游戏说明
                </button>
            </div>
            
            <!-- 装饰元素 -->
            <div class="absolute top-20 left-10 opacity-20 animate-float hidden md:block">
                <i class="fa fa-moon-o text-5xl text-primary"></i>
            </div>
            <div class="absolute bottom-20 right-10 opacity-20 animate-float" style="animation-delay: 2s;">
                <i class="fa fa-star text-5xl text-accent"></i>
            </div>
        </div>
        
        <!-- 帮助界面 -->
        <div id="help-screen" class="hidden">
            <button id="back-from-help" class="mb-6 px-4 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                <i class="fa fa-arrow-left mr-2"></i>返回
            </button>
            
            <h2 class="text-2xl font-bold mb-4 text-primary">游戏说明</h2>
            <div class="bg-dark/50 p-6 rounded-lg mb-6">
                <h3 class="text-xl font-semibold mb-3 text-accent">基本概念</h3>
                <ul class="list-disc pl-5 space-y-2 text-gray-300">
                    <li>修仙者有四个基本属性：根骨、悟性、气运和灵力</li>
                    <li>境界从低到高：炼气、筑基、金丹、元婴、化神、炼虚、合体、大乘、渡劫</li>
                    <li>每个境界有不同的寿元上限，突破境界可延长寿命</li>
                    <li>每个属性有当前境界下的极限值，突破极限需特殊机缘</li>
                </ul>
            </div>
            
            <div class="bg-dark/50 p-6 rounded-lg mb-6">
                <h3 class="text-xl font-semibold mb-3 text-accent">行动选择</h3>
                <ul class="list-disc pl-5 space-y-2 text-gray-300">
                    <li>修炼：增加修为，提升灵力</li>
                    <li>冥想：提升悟性，更容易领悟功法</li>
                    <li>历练：可能遇到机缘或危险，提升根骨</li>
                    <li>突破：尝试晋升到下一境界</li>
                </ul>
            </div>
            
            <div class="bg-dark/50 p-6 rounded-lg">
                <h3 class="text-xl font-semibold mb-3 text-accent">游戏目标</h3>
                <p class="text-gray-300">
                    不断提升境界，最终成功渡劫飞升，成为仙尊。注意寿元限制，
                    若寿元耗尽还未突破，将功亏一篑。
                </p>
            </div>
        </div>
        
        <!-- 游戏主界面 -->
        <div id="game-screen" class="hidden">
            <!-- 顶部信息 -->
            <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6">
                <div>
                    <h2 class="text-2xl font-bold text-primary">修仙之路</h2>
                    <p id="age-display" class="text-gray-400">年龄: <span>16</span> 岁 | 寿元: <span>100</span> 年</p>
                </div>
                <div class="mt-4 md:mt-0">
                    <button id="restart-btn" class="px-4 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                        <i class="fa fa-refresh mr-1"></i>重新开始
                    </button>
                </div>
            </div>
            
            <!-- 主要内容区 - 使用flex确保两侧等高 -->
            <div class="flex flex-col lg:flex-row gap-6">
                <!-- 左侧：角色状态 -->
                <div class="lg:w-1/3 flex flex-col gap-6">
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20 flex-1">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">角色状态</h3>
                        
                        <div class="flex justify-center mb-6">
                            <div class="w-32 h-32 rounded-full bg-primary/20 flex items-center justify-center animate-pulse-slow overflow-hidden">
                                <img id="cultivation-avatar" src="" alt="修仙者头像" class="w-full h-full object-cover">
                            </div>
                        </div>
                        
                        <div class="mb-4">
                            <p class="text-gray-400 mb-1">姓名</p>
                            <p id="name-display" class="text-xl font-medium">无尘子</p>
                        </div>
                        
                        <div class="mb-4">
                            <p class="text-gray-400 mb-1">境界</p>
                            <p id="realm-display" class="text-xl font-medium text-primary">炼气初期</p>
                        </div>
                        
                        <div class="mb-2">
                            <p class="text-gray-400 mb-1">修为进度</p>
                            <div class="w-full bg-dark rounded-full h-2.5">
                                <div id="cultivation-progress" class="bg-primary h-2.5 rounded-full" style="width: 20%"></div>
                            </div>
                            <p id="progress-text" class="text-right text-sm text-gray-500 mt-1">20/100</p>
                        </div>
                    </div>
                    
                    <!-- 属性面板 -->
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20 flex-1">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">基本属性</h3>
                        
                        <div class="space-y-4">
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">根骨</span>
                                    <span id="constitution-value" class="text-secondary">6</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="constitution-bar" style="width: 60%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响修炼效率和体魄强度</p>
                            </div>
                            
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">悟性</span>
                                    <span id="insight-value" class="text-secondary">4</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="insight-bar" style="width: 40%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响修炼速度和领悟能力</p>
                            </div>
                            
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">气运</span>
                                    <span id="luck-value" class="text-secondary">5</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="luck-bar" style="width: 50%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响随机事件的好坏</p>
                            </div>
                            
                            <div>
                                <div class="flex justify-between mb-1">
                                    <span class="text-gray-300">灵力</span>
                                    <span id="spirit-value" class="text-secondary">3</span>
                                </div>
                                <div class="w-full bg-dark rounded-full h-2">
                                    <div class="bg-secondary h-2 rounded-full" id="spirit-bar" style="width: 30%"></div>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">影响战斗能力和法术威力</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 右侧：行动和日志 -->
                <div class="lg:w-2/3 flex flex-col gap-6">
                    <!-- 行动选择 -->
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">行动选择</h3>
                        
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <button id="meditate-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-moon-o text-2xl mb-2"></i>
                                <span>静心冥想</span>
                                <span class="text-xs text-gray-400 mt-1">提升悟性，领悟功法</span>
                            </button>
                            
                            <button id="cultivate-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-fire text-2xl mb-2"></i>
                                <span>潜心修炼</span>
                                <span class="text-xs text-gray-400 mt-1">增加修为，提升灵力</span>
                            </button>
                            
                            <button id="adventure-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-compass text-2xl mb-2"></i>
                                <span>外出历练</span>
                                <span class="text-xs text-gray-400 mt-1">遭遇机缘或危险，提升根骨</span>
                            </button>
                            
                            <button id="breakthrough-btn" class="action-btn bg-primary/20 hover:bg-primary/30 text-white p-4 rounded-lg transition-all duration-300 flex flex-col items-center">
                                <i class="fa fa-rocket text-2xl mb-2"></i>
                                <span>尝试突破</span>
                                <span class="text-xs text-gray-400 mt-1">冲击下一境界，有失败风险</span>
                            </button>
                        </div>
                    </div>
                    
                    <!-- 事件日志 -->
                    <div class="bg-dark/50 rounded-xl p-6 border border-primary/20 flex-1">
                        <h3 class="text-xl font-bold mb-4 text-center text-accent">修仙日志</h3>
                        
                        <div id="event-log" class="h-[300px] md:h-[400px] overflow-y-auto pr-2 text-gray-300 space-y-3 custom-scrollbar">
                            <div class="p-2 bg-dark/70 rounded border-l-2 border-primary">
                                <p>你踏上了修仙之路，当前为炼气初期。</p>
                            </div>
                            <div class="p-2 bg-dark/70 rounded border-l-2 border-gray-500">
                                <p>师父告诉你："大道无情，修行在己。"</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 突破境界弹窗 -->
        <div id="breakthrough-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30 transform transition-all">
                <h3 class="text-2xl font-bold mb-4 text-center text-accent">境界突破</h3>
                
                <div id="breakthrough-message" class="text-center mb-6 text-gray-300">
                    你准备突破到筑基期，成功率60%。突破过程中可能遭遇心魔侵袭！
                </div>
                
                <div class="flex justify-center gap-4">
                    <button id="confirm-breakthrough" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all">
                        开始突破
                    </button>
                    <button id="cancel-breakthrough" class="px-6 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                        取消
                    </button>
                </div>
            </div>
        </div>
        
        <!-- 事件弹窗 -->
        <div id="event-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30">
                <h3 id="event-title" class="text-2xl font-bold mb-4 text-center text-accent">奇遇</h3>
                
                <div id="event-description" class="text-center mb-6 text-gray-300">
                    你在山中发现了一株千年灵芝，服用后根骨提升了2点！
                </div>
                
                <div id="event-choices" class="flex flex-col gap-3 mb-6 hidden">
                    <!-- 动态生成选择按钮 -->
                </div>
                
                <button id="close-event" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all w-full">
                    继续修仙
                </button>
            </div>
        </div>
        
        <!-- 属性突破弹窗 -->
        <div id="attribute-break-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30">
                <h3 class="text-2xl font-bold mb-4 text-center text-accent">属性瓶颈</h3>
                
                <div id="attribute-break-message" class="text-center mb-6 text-gray-300">
                    你的悟性已达当前境界极限，尝试突破瓶颈？成功率40%
                </div>
                
                <div class="flex justify-center gap-4">
                    <button id="confirm-attribute-break" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all">
                        尝试突破
                    </button>
                    <button id="cancel-attribute-break" class="px-6 py-2 bg-dark hover:bg-dark/80 text-white rounded-lg transition-all">
                        暂缓突破
                    </button>
                </div>
            </div>
        </div>
        
        <!-- 游戏结束弹窗 -->
        <div id="game-over-modal" class="fixed inset-0 bg-black/70 flex items-center justify-center z-50 hidden">
            <div class="bg-dark rounded-xl p-8 max-w-md w-full mx-4 border border-primary/30">
                <h3 id="game-over-title" class="text-2xl font-bold mb-4 text-center text-accent">寿终正寝</h3>
                
                <div id="game-over-message" class="text-center mb-6 text-gray-300">
                    你享年85岁，止步于金丹期，未能得道成仙。
                </div>
                
                <div class="mb-6 text-center">
                    <p class="text-gray-400">你的修仙成就</p>
                    <p id="final-achievement" class="text-xl font-medium text-primary">金丹真人</p>
                </div>
                
                <button id="restart-game" class="px-6 py-2 bg-primary hover:bg-primary/80 text-white rounded-lg transition-all w-full">
                    重新开始
                </button>
            </div>
        </div>
    </div>

    <script>
        // 游戏数据
        const gameData = {
            name: "无尘子",
            age: 16,
            lifespan: 100,
            realm: 0, // 0:炼气, 1:筑基, 2:金丹, 3:元婴, 4:化神, 5:炼虚, 6:合体, 7:大乘, 8:渡劫
            cultivation: 20, // 当前修为
            maxCultivation: 100, // 当前境界所需修为
            attributes: {
                constitution: 6, // 根骨
                insight: 4, // 悟性
                luck: 5, // 气运
                spirit: 3 // 灵力
            },
            attributeLimits: {
                constitution: 15, // 根骨上限
                insight: 15, // 悟性上限
                luck: 15, // 气运上限
                spirit: 15 // 灵力上限
            },
            log: [
                { text: "你踏上了修仙之路，当前为炼气初期。", type: "major" },
                { text: "师父告诉你：\"大道无情，修行在己。\"", type: "normal" }
            ]
        };
        
        // 境界数据
        const realms = [
            { name: "炼气初期", maxAge: 100, next: "炼气中期", required: 100, limitFactor: 1 },
            { name: "炼气中期", maxAge: 120, next: "炼气后期", required: 200, limitFactor: 1.2 },
            { name: "炼气后期", maxAge: 150, next: "筑基初期", required: 300, limitFactor: 1.4 },
            { name: "筑基初期", maxAge: 200, next: "筑基中期", required: 500, limitFactor: 2 },
            { name: "筑基中期", maxAge: 250, next: "筑基后期", required: 800, limitFactor: 2.3 },
            { name: "筑基后期", maxAge: 300, next: "金丹初期", required: 1200, limitFactor: 2.6 },
            { name: "金丹初期", maxAge: 500, next: "金丹中期", required: 2000, limitFactor: 3 },
            { name: "金丹中期", maxAge: 600, next: "金丹后期", required: 3000, limitFactor: 3.4 },
            { name: "金丹后期", maxAge: 800, next: "元婴初期", required: 5000, limitFactor: 3.8 },
            { name: "元婴初期", maxAge: 1000, next: "元婴中期", required: 8000, limitFactor: 4 },
            { name: "元婴中期", maxAge: 1200, next: "元婴后期", required: 12000, limitFactor: 4.3 },
            { name: "元婴后期", maxAge: 1500, next: "化神初期", required: 20000, limitFactor: 4.6 },
            { name: "化神初期", maxAge: 2000, next: "化神中期", required: 30000, limitFactor: 5 },
            { name: "化神中期", maxAge: 2500, next: "化神后期", required: 50000, limitFactor: 5.4 },
            { name: "化神后期", maxAge: 3000, next: "炼虚初期", required: 80000, limitFactor: 5.8 },
            { name: "炼虚初期", maxAge: 5000, next: "炼虚中期", required: 120000, limitFactor: 6 },
            { name: "炼虚中期", maxAge: 6000, next: "炼虚后期", required: 200000, limitFactor: 6.4 },
            { name: "炼虚后期", maxAge: 8000, next: "合体初期", required: 300000, limitFactor: 6.8 },
            { name: "合体初期", maxAge: 10000, next: "合体中期", required: 500000, limitFactor: 7 },
            { name: "合体中期", maxAge: 15000, next: "合体后期", required: 800000, limitFactor: 7.5 },
            { name: "合体后期", maxAge: 20000, next: "大乘初期", required: 1200000, limitFactor: 8 },
            { name: "大乘初期", maxAge: 50000, next: "大乘中期", required: 2000000, limitFactor: 8.5 },
            { name: "大乘中期", maxAge: 80000, next: "大乘后期", required: 3000000, limitFactor: 9 },
            { name: "大乘后期", maxAge: 100000, next: "渡劫期", required: 5000000, limitFactor: 9.5 },
            { name: "渡劫期", maxAge: 200000, next: "飞升", required: 10000000, limitFactor: 10 }
        ];
        
        // 根据境界获取基础属性上限
        function getBaseAttributeLimit() {
            return {
                constitution: 15 * realms[gameData.realm].limitFactor,
                insight: 15 * realms[gameData.realm].limitFactor,
                luck: 15 * realms[gameData.realm].limitFactor,
                spirit: 15 * realms[gameData.realm].limitFactor
            };
        }
        
        // 随机事件系统 - 按境界分组
        const eventSystem = {
            // 境界分组: 0-2=低阶(炼气), 3-8=中阶(筑基-金丹), 9-20=高阶(元婴-大乘), 21-=渡劫
            groups: {
                low: 0,    // 炼气阶段
                mid: 3,    // 筑基阶段开始
                high: 9,   // 元婴阶段开始
                tribulation: 21 // 渡劫阶段开始
            },
            
            // 通用事件 - 所有境界都可能发生
            commonEvents: [
                {
                    id: "meditation_enhance",
                    name: "禅定精进",
                    description: "你在冥想中进入了深层次的禅定状态。",
                    effect: function() {
                        attemptAttributeGain('insight', 1);
                    },
                    probability: 10,
                    type: "good"
                },
                {
                    id: "bad_dream",
                    name: "心魔入梦",
                    description: "夜晚被噩梦困扰，心神不宁。",
                    effect: function() {
                        attemptAttributeLoss('insight', 1);
                        addLog("心魔入梦，心神不宁，悟性略有下降。", "bad");
                    },
                    probability: 8,
                    type: "bad"
                }
            ],
            
            // 低阶事件 (炼气阶段)
            lowEvents: [
                {
                    id: "herb_discover",
                    name: "发现草药",
                    description: "在山中发现了几株有助于修炼的草药。",
                    effect: function() {
                        const cultGain = Math.floor(Math.random() * 20) + 10;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`发现了珍贵草药，服用后修为增加${cultGain}点。`, "good");
                    },
                    probability: 12,
                    type: "good"
                },
                {
                    id: "basic_manual",
                    name: "获得基础功法",
                    description: "在藏经阁找到了一本基础修炼法门。",
                    effect: function() {
                        attemptAttributeGain('insight', 1);
                        addLog("研读基础功法，对修炼有了更深的理解。", "good");
                    },
                    probability: 10,
                    type: "good"
                },
                {
                    id: "senior_guidance",
                    name: "前辈指点",
                    description: "一位门派前辈对你的修炼方法给予了指点。",
                    effect: function() {
                        const cultGain = Math.floor(Math.random() * 30) + 20;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`得到前辈指点，修为大增${cultGain}点。`, "good");
                    },
                    probability: 8,
                    type: "good"
                },
                {
                    id: "practice_injury",
                    name: "修炼岔气",
                    description: "修炼时不慎岔气，伤及经脉。",
                    effect: function() {
                        attemptAttributeLoss('constitution', 1);
                        const cultLoss = Math.floor(Math.random() * 15) + 5;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog(`修炼岔气伤及经脉，根骨受损，修为减少${cultLoss}点。`, "bad");
                    },
                    probability: 10,
                    type: "bad"
                },
                {
                    id: "steal_qi",
                    name: "灵气被吸",
                    description: "修炼之地的灵气被附近妖物偷偷吸收。",
                    effect: function() {
                        const cultLoss = Math.floor(Math.random() * 20) + 10;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog(`修炼时灵气被妖物窃取，修为减少${cultLoss}点。`, "bad");
                    },
                    probability: 9,
                    type: "bad"
                },
                {
                    id: "low_choice_1",
                    name: "功法选择",
                    description: "你获得了两本基础功法，只能选择其一修炼：",
                    choices: [
                        {
                            text: "《锻体诀》",
                            effect: function() {
                                attemptAttributeGain('constitution', 2);
                                addLog("选择修炼《锻体诀》，体质得到增强。", "good");
                            }
                        },
                        {
                            text: "《纳气法》",
                            effect: function() {
                                attemptAttributeGain('spirit', 2);
                                addLog("选择修炼《纳气法》，灵力得到提升。", "good");
                            }
                        }
                    ],
                    probability: 10,
                    type: "choice"
                },
                {
                    id: "low_choice_2",
                    name: "资源分配",
                    description: "门派发放了有限的修炼资源，你选择：",
                    choices: [
                        {
                            text: "专注提升修为",
                            effect: function() {
                                const cultGain = Math.floor(Math.random() * 50) + 30;
                                gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                                addLog(`专注提升修为，获得${cultGain}点修为增长。`, "good");
                            }
                        },
                        {
                            text: "打磨自身悟性",
                            effect: function() {
                                attemptAttributeGain('insight', 2);
                                addLog("选择打磨悟性，对修炼之道有了新的领悟。", "good");
                            }
                        }
                    ],
                    probability: 9,
                    type: "choice"
                },
                // 更多低阶事件...
                { id: "low_event_8", name: "初入山门", description: "作为新入门弟子，你收到了一些基础修炼物资。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 25) + 15;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`获得基础修炼物资，修为增加${cultGain}点。`, "good");
                }, probability: 11, type: "good" },
                { id: "low_event_9", name: "同门切磋", description: "与同门弟子切磋武艺，获益良多。", effect: function() {
                    attemptAttributeGain('spirit', 1);
                    addLog("与同门切磋，战斗技巧提升，灵力有所增长。", "good");
                }, probability: 10, type: "good" },
                { id: "low_event_10", name: "法器损坏", description: "修炼用的基础法器意外损坏。", effect: function() {
                    const cultLoss = Math.floor(Math.random() * 25) + 10;
                    gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                    addLog(`修炼法器损坏，修炼进度受阻，修为减少${cultLoss}点。`, "bad");
                }, probability: 8, type: "bad" },
                { id: "low_event_11", name: "天气影响", description: "连续多日阴雨，灵气稀薄，影响修炼。", effect: function() {
                    gameData.age += 1;
                    addLog("天气不佳，灵气稀薄，白白浪费了一年时间。", "bad");
                }, probability: 7, type: "bad" },
                { id: "low_event_12", name: "偶遇灵宠", description: "在山中遇到一只小型灵宠，与你颇为投缘。", effect: function() {
                    attemptAttributeGain('luck', 2);
                    addLog("灵宠相随，气运有所提升。", "good");
                }, probability: 6, type: "good" },
                { id: "low_event_13", name: "功法误读", description: "修炼时对功法理解出现偏差。", effect: function() {
                    attemptAttributeLoss('insight', 1);
                    const cultLoss = Math.floor(Math.random() * 15) + 5;
                    gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                    addLog(`功法理解出现偏差，悟性下降，修为减少${cultLoss}点。`, "bad");
                }, probability: 9, type: "bad" },
                { id: "low_event_14", name: "参加门派试炼", description: "门派举办入门试炼，你决定是否参加：", choices: [
                    { text: "参加试炼", effect: function() {
                        if (Math.random() < 0.6) {
                            const cultGain = Math.floor(Math.random() * 60) + 40;
                            gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                            attemptAttributeGain('constitution', 1);
                            addLog("在门派试炼中表现优异，修为大增，根骨也有所提升。", "good");
                        } else {
                            attemptAttributeLoss('constitution', 1);
                            const cultLoss = Math.floor(Math.random() * 20) + 10;
                            gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                            addLog("试炼中受伤，根骨受损，修为减少。", "bad");
                        }
                        gameData.age += 1;
                    }},
                    { text: "专注修炼", effect: function() {
                        const cultGain = Math.floor(Math.random() * 30) + 20;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`放弃试炼专注修炼，获得${cultGain}点修为。`, "normal");
                    }}
                ], probability: 10, type: "choice" },
                { id: "low_event_15", name: "发现隐藏洞穴", description: "在采药时意外发现一个隐藏的洞穴。", effect: function() {
                    const luckCheck = Math.random();
                    if (luckCheck < 0.5) {
                        const cultGain = Math.floor(Math.random() * 50) + 30;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`洞穴中灵气充沛，修炼效果倍增，获得${cultGain}点修为。`, "good");
                    } else if (luckCheck < 0.8) {
                        attemptAttributeGain('spirit', 2);
                        addLog("在洞穴中获得一枚灵石，吸收后灵力大增。", "good");
                    } else {
                        attemptAttributeLoss('constitution', 1);
                        addLog("洞穴中有毒气，吸入后伤及身体。", "bad");
                    }
                }, probability: 8, type: "mixed" },
                { id: "low_event_16", name: "收到家书", description: "收到家中来信，得知亲人近况。", effect: function() {
                    if (Math.random() < 0.7) {
                        addLog("家中一切安好，你放下心事，修炼更加专注。", "good");
                        const cultGain = Math.floor(Math.random() * 15) + 5;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    } else {
                        addLog("家中有事让你分心，修炼受到影响。", "bad");
                        const cultLoss = Math.floor(Math.random() * 15) + 5;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                    }
                }, probability: 7, type: "mixed" },
                { id: "low_event_17", name: "学习炼丹", description: "你有机会学习基础炼丹术：", choices: [
                    { text: "学习炼丹", effect: function() {
                        attemptAttributeGain('insight', 1);
                        gameData.age += 2;
                        addLog("花费两年时间学习炼丹术，虽然耽误了修炼，但悟性有所提升。", "mixed");
                    }},
                    { text: "专注修炼", effect: function() {
                        const cultGain = Math.floor(Math.random() * 40) + 20;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`放弃学习炼丹，专注修炼获得${cultGain}点修为。`, "normal");
                    }}
                ], probability: 9, type: "choice" },
                { id: "low_event_18", name: "遭遇野兽", description: "下山采购时遭遇凶猛野兽。", effect: function() {
                    if (gameData.attributes.spirit > 5) {
                        addLog("你运用灵力击退了野兽，毫发无伤。", "normal");
                    } else {
                        attemptAttributeLoss('constitution', 1);
                        gameData.age += 1;
                        addLog("为了躲避野兽，受伤并耽误了一年时间。", "bad");
                    }
                }, probability: 10, type: "mixed" },
                { id: "low_event_19", name: "获得传承", description: "在打扫祖师堂时，发现了一卷前人留下的修炼心得。", effect: function() {
                    attemptAttributeGain('insight', 2);
                    addLog("研读前人修炼心得，悟性大幅提升。", "good");
                }, probability: 5, type: "good" },
                { id: "low_event_20", name: "修炼宝地", description: "你发现了一处灵气相对浓郁的修炼点。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 40) + 20;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`在灵气浓郁处修炼，获得${cultGain}点修为。`, "good");
                }, probability: 9, type: "good" }
            ],
            
            // 中阶事件 (筑基-金丹阶段)
            midEvents: [
                {
                    id: "spiritual_spring",
                    name: "灵泉洗礼",
                    description: "发现一处蕴含浓郁灵气的泉眼，在其中沐浴后修为大进。",
                    effect: function() {
                        const cultGain = Math.floor(Math.random() * 100) + 80;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`在灵泉中沐浴，修为大幅增加${cultGain}点。`, "good");
                    },
                    probability: 9,
                    type: "good"
                },
                {
                    id: "ancient_ruins",
                    name: "古迹探寻",
                    description: "你发现一处古老的修仙遗迹，从中获得了一本残缺的功法。",
                    effect: function() {
                        attemptAttributeGain('insight', 2);
                        addLog(`在古迹中获得残缺功法，悟性提升。`, "good");
                    },
                    probability: 7,
                    type: "good"
                },
                {
                    id: "monster_attack",
                    name: "遭遇妖兽",
                    description: "历练途中遭遇凶猛妖兽袭击，你奋力抵抗...",
                    effect: function() {
                        // 受伤程度受灵力影响，灵力越高受伤越轻
                        const injury = Math.max(1, 3 - Math.floor(gameData.attributes.spirit / 5));
                        attemptAttributeLoss('constitution', injury);
                        const cultLoss = Math.floor(Math.random() * 30) + 20 * injury;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        
                        let message = `遭遇妖兽袭击，`;
                        if (injury === 1) {
                            message += `受了点轻伤，根骨下降1点，修为减少${cultLoss}点。`;
                        } else if (injury === 2) {
                            message += `受了重伤，根骨下降2点，修为减少${cultLoss}点。`;
                        } else {
                            message += `身受重创，根骨下降3点，修为减少${cultLoss}点。`;
                        }
                        addLog(message, "bad");
                    },
                    probability: 10,
                    type: "bad"
                },
                {
                    id: "sect_call",
                    name: "门派征召",
                    description: "宗门发布紧急任务，需要弟子前往支援：",
                    choices: [
                        {
                            text: "接受任务",
                            effect: function() {
                                attemptAttributeGain('luck', 1);
                                const cultGain = Math.floor(Math.random() * 150) + 100;
                                gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                                gameData.age += 2; // 任务耗时2年
                                addLog(`完成门派任务，气运提升，修为增加${cultGain}点，但耗费了2年时间。`, "mixed");
                            }
                        },
                        {
                            text: "婉言拒绝",
                            effect: function() {
                                attemptAttributeLoss('luck', 1);
                                addLog(`拒绝门派任务，引起长老不满，气运下降。`, "bad");
                            }
                        }
                    ],
                    probability: 7,
                    type: "choice"
                },
                {
                    id: "treasure_map",
                    name: "藏宝图",
                    description: "你偶然得到了一张残缺的藏宝图。",
                    effect: function() {
                        if (Math.random() < 0.4 + gameData.attributes.luck / 100 * 3) {
                            // 成功找到宝藏
                            const rewardType = Math.random();
                            if (rewardType < 0.5) {
                                const cultGain = Math.floor(Math.random() * 200) + 150;
                                gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                                addLog(`根据藏宝图找到一处宝藏，获得大量修为，增加${cultGain}点！`, "good");
                            } else {
                                const attrGain = Math.floor(Math.random() * 3) + 1;
                                attemptAttributeGain('spirit', attrGain);
                                addLog(`根据藏宝图找到一件灵物，灵力提升${attrGain}点！`, "good");
                            }
                            gameData.age += 1; // 寻宝耗时1年
                        } else {
                            // 寻宝失败
                            attemptAttributeLoss('constitution', 1);
                            gameData.age += 2; // 浪费2年时间
                            addLog("按照藏宝图寻找，却误入险境，不仅一无所获还受了伤，浪费了2年时间。", "bad");
                        }
                    },
                    probability: 8,
                    type: "mixed"
                },
                // 更多中阶事件...
                { id: "mid_event_6", name: "丹劫", description: "你炼制出一枚不错的丹药，但引来了小型丹劫。", effect: function() {
                    if (Math.random() < 0.6) {
                        const gain = Math.floor(Math.random() * 3) + 2;
                        attemptAttributeGain('constitution', gain);
                        addLog("成功渡过丹劫，服下丹药后根骨得到显著提升。", "good");
                    } else {
                        attemptAttributeLoss('constitution', 2);
                        const cultLoss = Math.floor(Math.random() * 50) + 30;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("丹劫中丹药被毁，你也受了伤，根骨下降，修为减少。", "bad");
                    }
                }, probability: 9, type: "mixed" },
                { id: "mid_event_7", name: "收服灵宠", description: "遇到一只潜力不错的妖兽，有机会将其收服。", effect: function() {
                    if (Math.random() < 0.5 + gameData.attributes.spirit / 100 * 2) {
                        attemptAttributeGain('luck', 2);
                        addLog("成功收服妖兽为灵宠，气运得到提升。", "good");
                    } else {
                        attemptAttributeLoss('spirit', 1);
                        const cultLoss = Math.floor(Math.random() * 30) + 20;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("收服失败，被妖兽所伤，灵力下降，修为减少。", "bad");
                    }
                }, probability: 8, type: "mixed" },
                { id: "mid_event_8", name: "功法突破", description: "你修炼的功法遇到了瓶颈，需要特殊机缘才能突破。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 200) + 100;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    attemptAttributeGain('insight', 2);
                    addLog("功法突破瓶颈，修为大增，悟性也得到提升。", "good");
                }, probability: 6, type: "good" },
                { id: "mid_event_9", name: "心魔劫", description: "修炼中遭遇心魔劫，险些走火入魔。", effect: function() {
                    if (Math.random() < 0.5 + gameData.attributes.insight / 100 * 2) {
                        attemptAttributeGain('insight', 1);
                        addLog("成功抵御心魔劫，心境更加稳固，悟性提升。", "good");
                    } else {
                        attemptAttributeLoss('insight', 2);
                        const cultLoss = Math.floor(Math.random() * 80) + 50;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("未能完全抵御心魔，悟性下降，修为大损。", "bad");
                    }
                }, probability: 10, type: "mixed" },
                { id: "mid_event_10", name: "秘境开启", description: "附近一处修炼秘境开启，你决定是否前往：", choices: [
                    { text: "进入秘境", effect: function() {
                        gameData.age += 3;
                        if (Math.random() < 0.5) {
                            const cultGain = Math.floor(Math.random() * 300) + 200;
                            gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                            const gain = Math.floor(Math.random() * 2) + 1;
                            attemptAttributeGain('spirit', gain);
                            addLog(`在秘境中收获丰厚，修为增加${cultGain}点，灵力提升${gain}点。`, "good");
                        } else {
                            attemptAttributeLoss('constitution', 2);
                            addLog("在秘境中遭遇危险，身受重伤，根骨下降。", "bad");
                        }
                    }},
                    { text: "留守修炼", effect: function() {
                        const cultGain = Math.floor(Math.random() * 150) + 100;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`选择留守修炼，获得${cultGain}点修为。`, "normal");
                    }}
                ], probability: 11, type: "choice" },
                { id: "mid_event_11", name: "结识道友", description: "在历练中结识了一位志同道合的道友。", effect: function() {
                    attemptAttributeGain('luck', 1);
                    const cultGain = Math.floor(Math.random() * 80) + 50;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog("与道友交流修炼心得，修为有所增长，气运也得到提升。", "good");
                }, probability: 9, type: "good" },
                { id: "mid_event_12", name: "夺宝之争", description: "发现一处宝物，但同时也有其他修士觊觎。", effect: function() {
                    if (Math.random() < 0.5 + gameData.attributes.spirit / 100 * 2) {
                        const gain = Math.floor(Math.random() * 3) + 1;
                        attemptAttributeGain('spirit', gain);
                        addLog(`成功夺得宝物，灵力提升${gain}点。`, "good");
                    } else {
                        attemptAttributeLoss('constitution', 1);
                        attemptAttributeLoss('spirit', 1);
                        const cultLoss = Math.floor(Math.random() * 60) + 40;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("夺宝失败，身受重伤，根骨和灵力都有下降。", "bad");
                    }
                }, probability: 10, type: "mixed" },
                { id: "mid_event_13", name: "功法失窃", description: "你辛苦钻研的功法心得被人偷走。", effect: function() {
                    attemptAttributeLoss('insight', 2);
                    const cultLoss = Math.floor(Math.random() * 100) + 50;
                    gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                    addLog("功法心得失窃，心血白费，悟性下降，修为减少。", "bad");
                }, probability: 7, type: "bad" },
                { id: "mid_event_14", name: "开坛讲法", description: "门派长老开坛讲法，你是否参加？", choices: [
                    { text: "参加讲法", effect: function() {
                        attemptAttributeGain('insight', 2);
                        gameData.age += 1;
                        addLog("参加长老讲法，受益匪浅，悟性提升。", "good");
                    }},
                    { text: "自行修炼", effect: function() {
                        const cultGain = Math.floor(Math.random() * 100) + 60;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`选择自行修炼，获得${cultGain}点修为。`, "normal");
                    }}
                ], probability: 8, type: "choice" },
                { id: "mid_event_15", name: "收服领地", description: "你发现一处适合修炼的山谷，想要将其占据。", effect: function() {
                    if (Math.random() < 0.6) {
                        const cultGain = Math.floor(Math.random() * 150) + 100;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`成功占据山谷，作为修炼之地，修为增长${cultGain}点。`, "good");
                    } else {
                        attemptAttributeLoss('constitution', 1);
                        gameData.age += 2;
                        addLog("争夺山谷失败，受伤并浪费了两年时间。", "bad");
                    }
                }, probability: 9, type: "mixed" },
                { id: "mid_event_16", name: "发现矿脉", description: "在山中发现一处灵石矿脉。", effect: function() {
                    const gain = Math.floor(Math.random() * 3) + 2;
                    attemptAttributeGain('luck', gain);
                    const cultGain = Math.floor(Math.random() * 120) + 80;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`发现灵石矿脉，气运提升${gain}点，修为增加${cultGain}点。`, "good");
                }, probability: 6, type: "good" },
                { id: "mid_event_17", name: "门派内斗", description: "门派内部出现派系争斗，你被卷入其中。", effect: function() {
                    if (Math.random() < 0.5) {
                        attemptAttributeGain('luck', 1);
                        const cultGain = Math.floor(Math.random() * 80) + 50;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("在门派内斗中站对了队伍，获得了奖励。", "good");
                    } else {
                        attemptAttributeLoss('luck', 2);
                        const cultLoss = Math.floor(Math.random() * 100) + 60;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("在门派内斗中失势，受到打压，气运下降，修为减少。", "bad");
                    }
                    gameData.age += 1;
                }, probability: 8, type: "mixed" },
                { id: "mid_event_18", name: "血脉觉醒", description: "修炼某种秘法时，你感觉体内似乎有某种力量被唤醒。", effect: function() {
                    const gain = Math.floor(Math.random() * 2) + 1;
                    attemptAttributeGain('constitution', gain);
                    attemptAttributeGain('spirit', gain);
                    addLog(`意外觉醒了潜藏的血脉之力，根骨和灵力各提升${gain}点。`, "good");
                }, probability: 5, type: "good" },
                { id: "mid_event_19", name: "阵法研究", description: "你对上古阵法产生了兴趣：", choices: [
                    { text: "深入研究", effect: function() {
                        attemptAttributeGain('insight', 2);
                        gameData.age += 2;
                        addLog("花费两年时间研究阵法，虽然耽误了修炼，但悟性大增。", "mixed");
                    }},
                    { text: "浅尝辄止", effect: function() {
                        attemptAttributeGain('insight', 1);
                        const cultLoss = Math.floor(Math.random() * 30) + 20;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("简单了解阵法知识，悟性略有提升，但修为受到影响。", "mixed");
                    }}
                ], probability: 7, type: "choice" },
                { id: "mid_event_20", name: "渡劫准备", description: "你开始为下一次境界突破做准备。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 150) + 100;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`为突破境界做准备，修为增加${cultGain}点，突破成功率略有提升。`, "good");
                }, probability: 10, type: "good" }
            ],
            
            // 高阶事件 (元婴-大乘阶段)
            highEvents: [
                {
                    id: "celestial_visit",
                    name: "仙人一顾",
                    description: "一位路过的仙人对你稍加指点。",
                    effect: function() {
                        const cultGain = Math.floor(Math.random() * 500) + 300;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        attemptAttributeGain('insight', 3);
                        addLog(`得到仙人指点，修为大增${cultGain}点，悟性提升3点！`, "major");
                    },
                    probability: 5,
                    type: "good"
                },
                {
                    id: "demon_invasion",
                    name: "魔气入侵",
                    description: "周边出现魔气侵染，影响修炼环境。",
                    effect: function() {
                        attemptAttributeLoss('spirit', 2);
                        const cultLoss = Math.floor(Math.random() * 200) + 100;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        gameData.age += 2;
                        addLog(`魔气侵染，修炼受阻，灵力下降2点，修为减少${cultLoss}点，浪费了2年时间。`, "bad");
                    },
                    probability: 8,
                    type: "bad"
                },
                {
                    id: "artifact_discover",
                    name: "法宝出世",
                    description: "一处古战场遗迹中有法宝出世，引来了各方势力争夺。",
                    effect: function() {
                        if (Math.random() < 0.3 + gameData.attributes.luck / 100 * 2) {
                            attemptAttributeGain('spirit', 5);
                            addLog("在法宝争夺中胜出，获得一件强大法宝，灵力大幅提升！", "major");
                        } else {
                            attemptAttributeLoss('constitution', 3);
                            const cultLoss = Math.floor(Math.random() * 300) + 200;
                            gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                            gameData.age += 3;
                            addLog(`争夺法宝失败，身受重伤，根骨下降3点，修为减少${cultLoss}点，浪费了3年时间。`, "bad");
                        }
                    },
                    probability: 7,
                    type: "mixed"
                },
                {
                    id: "preach_dharma",
                    name: "讲经说法",
                    description: "你修为渐深，开始为后辈弟子讲经说法。",
                    effect: function() {
                        attemptAttributeGain('insight', 2);
                        attemptAttributeGain('luck', 1);
                        const cultGain = Math.floor(Math.random() * 150) + 100;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("为后辈讲经说法，教学相长，悟性和气运都有提升。", "good");
                    },
                    probability: 9,
                    type: "good"
                },
                {
                    id: "high_choice_1",
                    name: "大道抉择",
                    description: "你的修炼遇到了关键路口，需要做出抉择：",
                    choices: [
                        {
                            text: "追求力量",
                            effect: function() {
                                attemptAttributeGain('spirit', 3);
                                attemptAttributeLoss('insight', 1);
                                addLog("选择追求力量，灵力大幅提升，但对道的理解略有偏差。", "mixed");
                            }
                        },
                        {
                            text: "探求真理",
                            effect: function() {
                                attemptAttributeGain('insight', 3);
                                attemptAttributeLoss('spirit', 1);
                                addLog("选择探求真理，悟性大幅提升，但灵力增长放缓。", "mixed");
                            }
                        }
                    ],
                    probability: 10,
                    type: "choice"
                },
                // 更多高阶事件...
                { id: "high_event_6", name: "开辟洞府", description: "你决定在一处灵脉汇聚之地开辟自己的洞府。", effect: function() {
                    gameData.age += 5;
                    const cultGain = Math.floor(Math.random() * 800) + 500;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    attemptAttributeGain('luck', 2);
                    addLog(`花费5年时间开辟洞府，从此有了稳定的修炼场所，修为增加${cultGain}点，气运提升2点。`, "good");
                }, probability: 8, type: "good" },
                { id: "high_event_7", name: "收服弟子", description: "你修为高深，有后辈修士希望拜你为师。", effect: function() {
                    if (Math.random() < 0.7) {
                        attemptAttributeGain('luck', 2);
                        addLog("收下一位天赋不错的弟子，为你处理杂务，气运提升。", "good");
                    } else {
                        attemptAttributeLoss('luck', 1);
                        addLog("所收弟子心术不正，给你惹了不少麻烦，气运下降。", "bad");
                    }
                }, probability: 9, type: "mixed" },
                { id: "high_event_8", name: "天劫预警", description: "你预感不久后将有天劫降临。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 600) + 400;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    attemptAttributeGain('constitution', 2);
                    addLog("提前感知到天劫，加紧修炼准备，修为和根骨都有提升。", "good");
                }, probability: 10, type: "good" },
                { id: "high_event_9", name: "空间裂缝", description: "修炼时意外遭遇空间裂缝，被卷入其中。", effect: function() {
                    if (Math.random() < 0.4) {
                        const gain = Math.floor(Math.random() * 4) + 2;
                        attemptAttributeGain('spirit', gain);
                        const cultGain = Math.floor(Math.random() * 1000) + 600;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        gameData.age += 5;
                        addLog(`在异空间中获得奇遇，灵力提升${gain}点，修为增加${cultGain}点，但耗时5年。`, "good");
                    } else {
                        attemptAttributeLoss('constitution', 3);
                        attemptAttributeLoss('spirit', 2);
                        gameData.age += 10;
                        addLog("在异空间中受尽苦难，根骨和灵力都大幅下降，浪费了10年时间。", "bad");
                    }
                }, probability: 6, type: "mixed" },
                { id: "high_event_10", name: "仙盟邀请", description: "你收到了仙盟的邀请，希望你加入：", choices: [
                    { text: "加入仙盟", effect: function() {
                        attemptAttributeGain('luck', 3);
                        const cultGain = Math.floor(Math.random() * 300) + 200;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("加入仙盟，获得更多资源和信息，气运提升，修为增加。", "good");
                    }},
                    { text: "保持独立", effect: function() {
                        attemptAttributeGain('insight', 2);
                        attemptAttributeGain('spirit', 2);
                        addLog("选择保持独立，潜心修炼，悟性和灵力都得到提升。", "good");
                    }}
                ], probability: 10, type: "choice" },
                { id: "high_event_11", name: "炼制分身", description: "你尝试炼制一具分身辅助修炼。", effect: function() {
                    gameData.age += 3;
                    if (Math.random() < 0.5) {
                        const cultGain = Math.floor(Math.random() * 500) + 300;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`成功炼制分身，修炼效率大增，修为增加${cultGain}点。`, "good");
                    } else {
                        attemptAttributeLoss('spirit', 3);
                        const cultLoss = Math.floor(Math.random() * 200) + 100;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        addLog("炼制分身失败，灵力大损，修为减少。", "bad");
                    }
                }, probability: 7, type: "mixed" },
                { id: "high_event_12", name: "发现仙界通道", description: "在一处禁地深处，你发现了一条通往仙界的不稳定通道。", effect: function() {
                    if (Math.random() < 0.2 + gameData.attributes.luck / 100) {
                        addLog("成功通过仙界通道，直接飞升成仙！", "major");
                        showGameOver(true);
                        return;
                    } else {
                        attemptAttributeLoss('constitution', 4);
                        attemptAttributeLoss('spirit', 3);
                        gameData.age += 5;
                        addLog("仙界通道不稳定，你被空间乱流所伤，根骨和灵力大幅下降。", "bad");
                    }
                }, probability: 5, type: "mixed" },
                { id: "high_event_13", name: "大道感悟", description: "你对天地大道有了新的感悟。", effect: function() {
                    const gain = Math.floor(Math.random() * 3) + 2;
                    attemptAttributeGain('insight', gain);
                    const cultGain = Math.floor(Math.random() * 700) + 400;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`对大道有了新的感悟，悟性提升${gain}点，修为增加${cultGain}点。`, "good");
                }, probability: 8, type: "good" },
                { id: "high_event_14", name: "魔道围攻", description: "一群魔道修士对你发起了围攻。", effect: function() {
                    if (Math.random() < 0.6 + gameData.attributes.spirit / 100 * 2) {
                        attemptAttributeGain('spirit', 2);
                        const cultGain = Math.floor(Math.random() * 400) + 200;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("成功击退魔道修士，战斗中灵力得到提升，修为也有增长。", "good");
                    } else {
                        attemptAttributeLoss('constitution', 3);
                        attemptAttributeLoss('spirit', 2);
                        const cultLoss = Math.floor(Math.random() * 500) + 300;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        gameData.age += 2;
                        addLog("与魔道修士大战受重伤，根骨和灵力下降，修为大损。", "bad");
                    }
                }, probability: 9, type: "mixed" },
                { id: "high_event_15", name: "创建势力", description: "你考虑是否创建自己的修炼势力：", choices: [
                    { text: "创建势力", effect: function() {
                        attemptAttributeGain('luck', 2);
                        gameData.age += 5;
                        addLog("花费5年时间创建了自己的势力，获得更多资源，气运提升。", "good");
                    }},
                    { text: "独来独往", effect: function() {
                        const gain = Math.floor(Math.random() * 3) + 2;
                        attemptAttributeGain('insight', gain);
                        attemptAttributeGain('spirit', gain);
                        addLog(`选择独来独往，潜心修炼，悟性和灵力各提升${gain}点。`, "good");
                    }}
                ], probability: 10, type: "choice" },
                { id: "high_event_16", name: "时间加速", description: "你发现一处时间流速不同的秘境。", effect: function() {
                    gameData.age += 1;
                    const cultGain = Math.floor(Math.random() * 1000) + 600;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`在时间秘境中修炼，外界一年秘境十年，修为增加${cultGain}点。`, "good");
                }, probability: 6, type: "good" },
                { id: "high_event_17", name: "本源损伤", description: "修炼时不慎伤及修为本源。", effect: function() {
                    attemptAttributeLoss('constitution', 2);
                    attemptAttributeLoss('spirit', 2);
                    const cultLoss = Math.floor(Math.random() * 600) + 400;
                    gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                    gameData.age += 3;
                    addLog("伤及修为本源，根骨和灵力下降，修为大损，需要3年恢复。", "bad");
                }, probability: 7, type: "bad" },
                { id: "high_event_18", name: "收服神兽", description: "遇到一只濒临死亡的上古神兽，你可以选择：", choices: [
                    { text: "出手相救", effect: function() {
                        gameData.age += 2;
                        attemptAttributeGain('luck', 4);
                        addLog("花费2年时间救治神兽，获得其友谊，气运大幅提升。", "good");
                    }},
                    { text: "取其内丹", effect: function() {
                        attemptAttributeGain('spirit', 3);
                        attemptAttributeLoss('luck', 2);
                        const cultGain = Math.floor(Math.random() * 500) + 300;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("取走神兽内丹，灵力大增，修为提升，但气运受损。", "mixed");
                    }}
                ], probability: 8, type: "choice" },
                { id: "high_event_19", name: "法则领悟", description: "你对空间法则有了初步领悟。", effect: function() {
                    attemptAttributeGain('insight', 3);
                    const cultGain = Math.floor(Math.random() * 800) + 500;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog("领悟空间法则，对修炼之道有了全新认识，悟性提升，修为大增。", "good");
                }, probability: 6, type: "good" },
                { id: "high_event_20", name: "渡劫失败", description: "一位道友渡劫失败，你前去探查。", effect: function() {
                    if (Math.random() < 0.5) {
                        attemptAttributeGain('insight', 2);
                        const cultGain = Math.floor(Math.random() * 400) + 200;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("从道友失败中吸取教训，悟性提升，修为增加。", "good");
                    } else {
                        attemptAttributeLoss('spirit', 2);
                        gameData.age += 1;
                        addLog("受到道友渡劫失败的波及，灵力下降，浪费了一年时间。", "bad");
                    }
                }, probability: 9, type: "mixed" }
            ],
            
            // 渡劫期事件
            tribulationEvents: [
                {
                    id: "heavenly_tribulation",
                    name: "天劫降临",
                    description: "九重天雷劫降临，考验你的道心与实力。",
                    effect: function() {
                        if (Math.random() < 0.3 + gameData.attributes.constitution / 100 * 3) {
                            const cultGain = Math.floor(Math.random() * 2000) + 1000;
                            gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                            attemptAttributeGain('constitution', 5);
                            addLog(`成功渡过天雷劫，肉身得到淬炼，根骨提升5点，修为增加${cultGain}点！`, "major");
                        } else {
                            attemptAttributeLoss('constitution', 5);
                            const cultLoss = Math.floor(Math.random() * 1500) + 800;
                            gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                            gameData.age += 5;
                            addLog(`渡劫失败，肉身受损严重，根骨下降5点，修为减少${cultLoss}点，需要5年恢复。`, "bad");
                        }
                    },
                    probability: 15,
                    type: "mixed"
                },
                {
                    id: "dao_test",
                    name: "道心考验",
                    description: "你的道心受到天地法则的考验。",
                    effect: function() {
                        if (Math.random() < 0.4 + gameData.attributes.insight / 100 * 2) {
                            attemptAttributeGain('insight', 5);
                            addLog("通过道心考验，对大道的理解更加深刻，悟性大幅提升！", "good");
                        } else {
                            attemptAttributeLoss('insight', 3);
                            gameData.age += 3;
                            addLog("道心考验失败，对道的理解出现偏差，悟性下降，浪费了3年时间。", "bad");
                        }
                    },
                    probability: 12,
                    type: "mixed"
                },
                {
                    id: "immortal_artifact",
                    name: "仙器碎片",
                    description: "你在一处上古战场遗迹中发现了一块仙器碎片。",
                    effect: function() {
                        attemptAttributeGain('spirit', 5);
                        const cultGain = Math.floor(Math.random() * 3000) + 2000;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`获得仙器碎片，灵力提升5点，修为增加${cultGain}点！`, "good");
                    },
                    probability: 8,
                    type: "good"
                },
                {
                    id: "tribulation_choice_1",
                    name: "飞升准备",
                    description: "你即将面临最终的飞升天劫，需要做出准备：",
                    choices: [
                        {
                            text: "淬炼肉身",
                            effect: function() {
                                attemptAttributeGain('constitution', 4);
                                gameData.age += 5;
                                addLog("花费5年时间淬炼肉身，为抵御天劫做准备，根骨提升4点。", "good");
                            }
                        },
                        {
                            text: "感悟法则",
                            effect: function() {
                                attemptAttributeGain('insight', 4);
                                gameData.age += 5;
                                addLog("花费5年时间感悟天地法则，对天劫有了更深理解，悟性提升4点。", "good");
                            }
                        }
                    ],
                    probability: 15,
                    type: "choice"
                },
                {
                    id: "celestial_interference",
                    name: "仙凡阻隔",
                    description: "感受到你即将飞升，仙界某些存在试图阻止你。",
                    effect: function() {
                        if (Math.random() < 0.4 + gameData.attributes.luck / 100 * 2) {
                            attemptAttributeGain('luck', 3);
                            addLog("成功冲破仙凡阻隔，气运更加旺盛，离飞升更近一步！", "good");
                        } else {
                            attemptAttributeLoss('luck', 3);
                            attemptAttributeLoss('spirit', 3);
                            const cultLoss = Math.floor(Math.random() * 2000) + 1000;
                            gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                            gameData.age += 5;
                            addLog("受到仙界存在干扰，气运和灵力下降，修为大损。", "bad");
                        }
                    },
                    probability: 10,
                    type: "mixed"
                },
                // 更多渡劫期事件...
                { id: "tribulation_event_6", name: "五行天劫", description: "五行属性的天劫降临，考验你的综合能力。", effect: function() {
                    if (Math.random() < 0.35 + gameData.attributes.spirit / 100 * 2) {
                        attemptAttributeGain('spirit', 4);
                        const cultGain = Math.floor(Math.random() * 2500) + 1500;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`成功渡过五行天劫，灵力大幅提升4点，修为增加${cultGain}点。`, "good");
                    } else {
                        attemptAttributeLoss('spirit', 4);
                        const cultLoss = Math.floor(Math.random() * 2000) + 1000;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        gameData.age += 6;
                        addLog("五行天劫中受挫，灵力下降4点，修为大损，需要6年恢复。", "bad");
                    }
                }, probability: 14, type: "mixed" },
                { id: "tribulation_event_7", name: "天道问询", description: "天道对你发出问询，考验你的道心。", effect: function() {
                    if (Math.random() < 0.4 + gameData.attributes.insight / 100 * 2) {
                        attemptAttributeGain('insight', 4);
                        attemptAttributeGain('luck', 2);
                        addLog("完美回答天道问询，道心更加坚定，悟性和气运都有提升。", "good");
                    } else {
                        attemptAttributeLoss('insight', 3);
                        gameData.age += 4;
                        addLog("未能完美回答天道问询，道心受损，悟性下降，浪费了4年时间。", "bad");
                    }
                }, probability: 12, type: "mixed" },
                { id: "tribulation_event_8", name: "飞升使者", description: "一位飞升使者出现在你面前。", effect: function() {
                    if (Math.random() < 0.5 + gameData.attributes.luck / 100) {
                        const cultGain = Math.floor(Math.random() * 3000) + 2000;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`得到飞升使者指点，修为大增${cultGain}点，对飞升有了更清晰的认识。`, "good");
                    } else {
                        attemptAttributeLoss('luck', 3);
                        addLog("未能通过飞升使者的考验，气运受损。", "bad");
                    }
                }, probability: 10, type: "mixed" },
                { id: "tribulation_event_9", name: "本源融合", description: "你尝试将自身修为与天地本源融合。", effect: function() {
                    gameData.age += 10;
                    if (Math.random() < 0.3 + gameData.attributes.insight / 100 * 2) {
                        attemptAttributeGain('insight', 5);
                        attemptAttributeGain('spirit', 5);
                        const cultGain = Math.floor(Math.random() * 5000) + 3000;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`花费10年时间成功融合天地本源，悟性和灵力各提升5点，修为增加${cultGain}点！`, "major");
                    } else {
                        attemptAttributeLoss('insight', 4);
                        attemptAttributeLoss('spirit', 4);
                        addLog("融合天地本源失败，悟性和灵力大幅下降，浪费了10年时间。", "bad");
                    }
                }, probability: 9, type: "mixed" },
                { id: "tribulation_event_10", name: "最终抉择", description: "飞升前的最后抉择，将决定你未来的道路：", choices: [
                    { text: "斩断尘缘", effect: function() {
                        attemptAttributeGain('spirit', 5);
                        attemptAttributeLoss('luck', 1);
                        addLog("斩断尘缘，心无牵挂，灵力大幅提升，但失去了一些气运。", "mixed");
                    }},
                    { text: "携道飞升", effect: function() {
                        attemptAttributeGain('insight', 5);
                        attemptAttributeLoss('constitution', 1);
                        addLog("选择携道飞升，带着对世间的理解，悟性大幅提升，但肉身略有损伤。", "mixed");
                    }}
                ], probability: 15, type: "choice" },
                { id: "tribulation_event_11", name: "仙位争夺", description: "你感应到有其他渡劫期修士也在准备飞升，仙位有限。", effect: function() {
                    if (Math.random() < 0.5 + gameData.attributes.spirit / 100 * 2) {
                        attemptAttributeGain('spirit', 3);
                        const cultGain = Math.floor(Math.random() * 2000) + 1500;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog("在仙位争夺中占据优势，灵力提升，修为增加。", "good");
                    } else {
                        attemptAttributeLoss('spirit', 3);
                        const cultLoss = Math.floor(Math.random() * 1500) + 1000;
                        gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                        gameData.age += 4;
                        addLog("仙位争夺中处于劣势，灵力下降，修为减少，浪费了4年时间。", "bad");
                    }
                }, probability: 13, type: "mixed" },
                { id: "tribulation_event_12", name: "轮回试炼", description: "你被拉入轮回试炼，体验百世人生。", effect: function() {
                    gameData.age += 1; // 外界只过了1年
                    attemptAttributeGain('insight', 6);
                    addLog("经历轮回试炼，体验百世人生，对道的理解前所未有的深刻，悟性大幅提升6点！", "good");
                }, probability: 7, type: "good" },
                { id: "tribulation_event_13", name: "混沌之气", description: "你在一处空间节点中捕捉到一丝混沌之气。", effect: function() {
                    attemptAttributeGain('constitution', 5);
                    attemptAttributeGain('spirit', 5);
                    const cultGain = Math.floor(Math.random() * 4000) + 2500;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`获得混沌之气淬炼，根骨和灵力各提升5点，修为增加${cultGain}点！`, "major");
                }, probability: 6, type: "good" },
                { id: "tribulation_event_14", name: "心魔劫", description: "最终的心魔劫降临，考验你是否真正无牵无挂。", effect: function() {
                    if (Math.random() < 0.4 + gameData.attributes.insight / 100 * 2) {
                        attemptAttributeGain('insight', 4);
                        addLog("成功渡过心魔劫，心境圆满，悟性提升4点。", "good");
                    } else {
                        attemptAttributeLoss('insight', 4);
                        attemptAttributeLoss('luck', 2);
                        gameData.age += 5;
                        addLog("心魔劫中受挫，心境受损，悟性和气运下降，浪费了5年时间。", "bad");
                    }
                }, probability: 14, type: "mixed" },
                { id: "tribulation_event_15", name: "飞升准备", description: "你需要选择一种方式度过最后的准备阶段：", choices: [
                    { text: "闭关修炼", effect: function() {
                        gameData.age += 10;
                        const cultGain = Math.floor(Math.random() * 6000) + 4000;
                        gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                        addLog(`花费10年时间闭关修炼，修为大幅增加${cultGain}点。`, "good");
                    }},
                    { text: "云游四方", effect: function() {
                        gameData.age += 10;
                        attemptAttributeGain('insight', 4);
                        attemptAttributeGain('luck', 3);
                        addLog("花费10年时间云游四方，体悟天地大道，悟性和气运都有提升。", "good");
                    }}
                ], probability: 15, type: "choice" },
                { id: "tribulation_event_16", name: "仙劫预警", description: "你感应到最终的飞升大劫即将来临。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 5000) + 3000;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`感应到飞升大劫临近，加紧修炼，修为增加${cultGain}点。`, "good");
                }, probability: 12, type: "good" },
                { id: "tribulation_event_17", name: "法则反噬", description: "领悟过深，引来天地法则反噬。", effect: function() {
                    attemptAttributeLoss('insight', 3);
                    attemptAttributeLoss('spirit', 3);
                    const cultLoss = Math.floor(Math.random() * 2000) + 1500;
                    gameData.cultivation = Math.max(gameData.cultivation - cultLoss, 0);
                    gameData.age += 6;
                    addLog("天地法则反噬，悟性和灵力下降，修为大损，需要6年恢复。", "bad");
                }, probability: 10, type: "bad" },
                { id: "tribulation_event_18", name: "仙缘汇聚", description: "无数年来积累的仙缘在你身上汇聚。", effect: function() {
                    attemptAttributeGain('luck', 5);
                    const cultGain = Math.floor(Math.random() * 4000) + 2500;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`仙缘汇聚，气运提升5点，修为增加${cultGain}点，飞升有望！`, "good");
                }, probability: 8, type: "good" },
                { id: "tribulation_event_19", name: "最后一战", description: "飞升前，你需要与宿敌进行最后一战：", choices: [
                    { text: "正面迎战", effect: function() {
                        if (Math.random() < 0.5 + gameData.attributes.spirit / 100 * 2) {
                            attemptAttributeGain('spirit', 4);
                            const cultGain = Math.floor(Math.random() * 3000) + 2000;
                            gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                            addLog("战胜宿敌，心结解开，灵力提升，修为增加。", "good");
                        } else {
                            attemptAttributeLoss('constitution', 4);
                            attemptAttributeLoss('spirit', 3);
                            gameData.age += 5;
                            addLog("与宿敌大战受重伤，根骨和灵力下降，浪费了5年时间。", "bad");
                        }
                    }},
                    { text: "化解恩怨", effect: function() {
                        attemptAttributeGain('insight', 4);
                        attemptAttributeGain('luck', 2);
                        addLog("成功化解与宿敌的恩怨，心境提升，悟性和气运都有增加。", "good");
                    }}
                ], probability: 14, type: "choice" },
                { id: "tribulation_event_20", name: "飞升之门", description: "感应到飞升之门即将开启。", effect: function() {
                    const cultGain = Math.floor(Math.random() * 6000) + 4000;
                    gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
                    addLog(`飞升之门即将开启，你感受到无尽的仙力牵引，修为大幅增加${cultGain}点！`, "major");
                }, probability: 10, type: "good" }
            ],
            
            // 获取当前境界的随机事件
            getRandomEvent: function() {
                // 根据当前境界选择事件池
                let eventPool = [...this.commonEvents]; // 总是包含通用事件
                
                if (gameData.realm >= this.groups.tribulation) {
                    eventPool = [...eventPool, ...this.tribulationEvents];
                } else if (gameData.realm >= this.groups.high) {
                    eventPool = [...eventPool, ...this.highEvents];
                } else if (gameData.realm >= this.groups.mid) {
                    eventPool = [...eventPool, ...this.midEvents];
                } else {
                    eventPool = [...eventPool, ...this.lowEvents];
                }
                
                // 根据气运调整事件概率
                const luckFactor = gameData.attributes.luck / 5; // 气运影响因子
                
                // 创建加权事件列表
                const weightedEvents = [];
                eventPool.forEach(event => {
                    let adjustedProbability = event.probability;
                    
                    // 根据事件类型和运气调整概率
                    if (event.type === "good") {
                        adjustedProbability *= luckFactor; // 运气好，好事概率增加
                    } else if (event.type === "bad") {
                        adjustedProbability *= (2 - luckFactor); // 运气好，坏事概率减少
                    }
                    
                    // 确保概率为正数
                    adjustedProbability = Math.max(1, Math.round(adjustedProbability));
                    
                    // 添加到加权列表
                    for (let i = 0; i < adjustedProbability; i++) {
                        weightedEvents.push(event);
                    }
                });
                
                // 随机选择一个事件
                return weightedEvents[Math.floor(Math.random() * weightedEvents.length)];
            }
        };
        
        // 尝试提升属性，考虑属性上限
        function attemptAttributeGain(attribute, amount) {
            const baseLimit = getBaseAttributeLimit()[attribute];
            const currentValue = gameData.attributes[attribute];
            const currentLimit = gameData.attributeLimits[attribute];
            
            // 计算实际上限（基础上限和当前限制的最大值）
            const effectiveLimit = Math.max(baseLimit, currentLimit);
            
            // 如果当前值离上限还有空间
            if (currentValue + amount <= effectiveLimit) {
                gameData.attributes[attribute] += amount;
                addLog(`${getAttributeName(attribute)}提升了${amount}点。`, "good");
                return true;
            } 
            // 如果超出上限但在可尝试突破范围内
            else if (currentValue < effectiveLimit * 1.1) {
                // 显示属性突破弹窗
                const breakthroughSuccessRate = 30 + gameData.attributes.insight / effectiveLimit * 30;
                document.getElementById('attribute-break-message').textContent = 
                    `你的${getAttributeName(attribute)}已达当前境界极限(${Math.floor(effectiveLimit)})，尝试突破瓶颈？成功率${Math.round(breakthroughSuccessRate)}%`;
                
                // 存储当前要突破的属性和数量
                window.pendingAttribute = { attribute, amount };
                
                document.getElementById('attribute-break-modal').classList.remove('hidden');
                return false;
            }
            // 已远超上限，无法提升
            else {
                addLog(`${getAttributeName(attribute)}已达当前境界极限，无法继续提升。`, "bad");
                return false;
            }
        }
        
        // 尝试降低属性
        function attemptAttributeLoss(attribute, amount) {
            gameData.attributes[attribute] = Math.max(1, gameData.attributes[attribute] - amount);
            return true;
        }
        
        // 获取属性的中文名称
        function getAttributeName(attribute) {
            const names = {
                constitution: "根骨",
                insight: "悟性",
                luck: "气运",
                spirit: "灵力"
            };
            return names[attribute] || attribute;
        }
        
        // 处理属性突破结果
        function handleAttributeBreak(success) {
            if (!window.pendingAttribute) return;
            
            const { attribute, amount } = window.pendingAttribute;
            const baseLimit = getBaseAttributeLimit()[attribute];
            const currentValue = gameData.attributes[attribute];
            const currentLimit = gameData.attributeLimits[attribute];
            const effectiveLimit = Math.max(baseLimit, currentLimit);
            
            if (success) {
                // 突破成功
                gameData.attributes[attribute] = Math.min(currentValue + amount, effectiveLimit * 1.1);
                // 永久提升该属性上限
                gameData.attributeLimits[attribute] = effectiveLimit * 1.05;
                addLog(`${getAttributeName(attribute)}突破成功，提升了${amount}点，上限也有所提高！`, "major");
            } else {
                // 突破失败，随机降低一个属性
                const attributes = Object.keys(gameData.attributes);
                const randomAttr = attributes[Math.floor(Math.random() * attributes.length)];
                gameData.attributes[randomAttr] = Math.max(1, gameData.attributes[randomAttr] - 1);
                addLog(`${getAttributeName(attribute)}突破失败，${getAttributeName(randomAttr)}下降了1点。`, "bad");
            }
            
            window.pendingAttribute = null;
            document.getElementById('attribute-break-modal').classList.add('hidden');
        }
        
        // DOM元素
        const startScreen = document.getElementById('start-screen');
        const helpScreen = document.getElementById('help-screen');
        const gameScreen = document.getElementById('game-screen');
        const startBtn = document.getElementById('start-btn');
        const helpBtn = document.getElementById('help-btn');
        const backFromHelpBtn = document.getElementById('back-from-help');
        const restartBtn = document.getElementById('restart-btn');
        const restartGameBtn = document.getElementById('restart-game');
        
        // 行动按钮
        const meditateBtn = document.getElementById('meditate-btn');
        const cultivateBtn = document.getElementById('cultivate-btn');
        const adventureBtn = document.getElementById('adventure-btn');
        const breakthroughBtn = document.getElementById('breakthrough-btn');
        
        // 弹窗相关
        const breakthroughModal = document.getElementById('breakthrough-modal');
        const confirmBreakthroughBtn = document.getElementById('confirm-breakthrough');
        const cancelBreakthroughBtn = document.getElementById('cancel-breakthrough');
        const eventModal = document.getElementById('event-modal');
        const eventTitle = document.getElementById('event-title');
        const eventDescription = document.getElementById('event-description');
        const eventChoices = document.getElementById('event-choices');
        const closeEventBtn = document.getElementById('close-event');
        const attributeBreakModal = document.getElementById('attribute-break-modal');
        const confirmAttributeBreakBtn = document.getElementById('confirm-attribute-break');
        const cancelAttributeBreakBtn = document.getElementById('cancel-attribute-break');
        const gameOverModal = document.getElementById('game-over-modal');
        const gameOverTitle = document.getElementById('game-over-title');
        const gameOverMessage = document.getElementById('game-over-message');
        const finalAchievement = document.getElementById('final-achievement');
        
        // 初始化事件监听
        function initEventListeners() {
            startBtn.addEventListener('click', startGame);
            helpBtn.addEventListener('click', showHelp);
            backFromHelpBtn.addEventListener('click', hideHelp);
            restartBtn.addEventListener('click', restartGame);
            restartGameBtn.addEventListener('click', restartGame);
            
            // 行动按钮
            meditateBtn.addEventListener('click', meditate);
            cultivateBtn.addEventListener('click', cultivate);
            adventureBtn.addEventListener('click', adventure);
            breakthroughBtn.addEventListener('click', showBreakthroughModal);
            
            // 弹窗按钮
            confirmBreakthroughBtn.addEventListener('click', attemptBreakthrough);
            cancelBreakthroughBtn.addEventListener('click', hideBreakthroughModal);
            closeEventBtn.addEventListener('click', hideEventModal);
            confirmAttributeBreakBtn.addEventListener('click', () => handleAttributeBreak(true));
            cancelAttributeBreakBtn.addEventListener('click', () => handleAttributeBreak(false));
        }
        
        // 开始游戏
        function startGame() {
            startScreen.classList.add('hidden');
            gameScreen.classList.remove('hidden');
            updateUI();
            
            // 更新头像
            updateAvatar();
        }
        
        // 显示帮助
        function showHelp() {
            startScreen.classList.add('hidden');
            helpScreen.classList.remove('hidden');
        }
        
        // 隐藏帮助
        function hideHelp() {
            helpScreen.classList.add('hidden');
            startScreen.classList.remove('hidden');
        }
        
        // 重新开始游戏
        function restartGame() {
            // 重置游戏数据
            gameData.age = 16;
            gameData.realm = 0;
            gameData.cultivation = 20;
            gameData.maxCultivation = realms[0].required;
            gameData.lifespan = realms[0].maxAge;
            gameData.attributes = {
                constitution: Math.floor(Math.random() * 5) + 3,
                insight: Math.floor(Math.random() * 5) + 3,
                luck: Math.floor(Math.random() * 5) + 3,
                spirit: Math.floor(Math.random() * 5) + 3
            };
            gameData.attributeLimits = {
                constitution: 15,
                insight: 15,
                luck: 15,
                spirit: 15
            };
            gameData.log = [
                { text: "你踏上了修仙之路，当前为炼气初期。", type: "major" },
                { text: "师父告诉你：\"大道无情，修行在己。\"", type: "normal" }
            ];
            
            // 隐藏所有弹窗
            breakthroughModal.classList.add('hidden');
            eventModal.classList.add('hidden');
            attributeBreakModal.classList.add('hidden');
            gameOverModal.classList.add('hidden');
            
            // 显示游戏界面
            startScreen.classList.add('hidden');
            helpScreen.classList.add('hidden');
            gameScreen.classList.remove('hidden');
            
            updateUI();
        }
        
        // 更新UI显示
        function updateUI() {
            // 更新年龄和寿元
            document.querySelector('#age-display span:first-child').textContent = gameData.age;
            document.querySelector('#age-display span:last-child').textContent = gameData.lifespan;
            
            // 更新姓名和境界
            document.getElementById('name-display').textContent = gameData.name;
            document.getElementById('realm-display').textContent = realms[gameData.realm].name;
            
            // 更新修为进度
            const progressPercent = Math.min(100, (gameData.cultivation / gameData.maxCultivation) * 100);
            document.getElementById('cultivation-progress').style.width = `${progressPercent}%`;
            document.getElementById('progress-text').textContent = `${gameData.cultivation}/${gameData.maxCultivation}`;
            
            // 更新属性
            for (const [key, value] of Object.entries(gameData.attributes)) {
                document.getElementById(`${key}-value`).textContent = Math.floor(value);
                const baseLimit = getBaseAttributeLimit()[key];
                const currentLimit = gameData.attributeLimits[key];
                const effectiveLimit = Math.max(baseLimit, currentLimit);
                const percent = Math.min(100, (value / effectiveLimit) * 100);
                document.getElementById(`${key}-bar`).style.width = `${percent}%`;
            }
            
            // 更新日志
            updateLog();
        }
        
        // 更新头像
        function updateAvatar() {
             // 生成并打印 1-9 的随机整数
            const randomIndex = Math.floor(Math.random() * 9) + 1;
            const avatarElement = document.getElementById('cultivation-avatar');
            // 根据不同境界使用不同的头像
            // let seed = "cultivator";
            // if (gameData.realm >= 3) seed = "foundation";
            // if (gameData.realm >= 6) seed = "golden-core";
            // if (gameData.realm >= 9) seed = "nascent-soul";
            // if (gameData.realm >= 13) seed = "spirit-transform";
            // if (gameData.realm >= 21) seed = "tribulation";
            
            // avatarElement.src = `https://picsum.photos/seed/${seed}/200/200`;
            // avatarElement.alt = `${realms[gameData.realm].name}修仙者形象`;

            avatarElement.src = `https://gitee.com/linghuzhe/immortal-cultivation-simulator/raw/master/cultivation-simulator/pic/avatar${randomIndex}.png`;
        }
        
        // 更新日志显示
        function updateLog() {
            const logElement = document.getElementById('event-log');
            logElement.innerHTML = '';
            
            gameData.log.forEach(entry => {
                const logItem = document.createElement('div');
                logItem.className = 'p-2 bg-dark/70 rounded border-l-2';
                
                // 根据日志类型设置不同颜色
                if (entry.type === "major") {
                    logItem.classList.add('border-primary');
                } else if (entry.type === "good") {
                    logItem.classList.add('border-secondary');
                } else if (entry.type === "bad") {
                    logItem.classList.add('border-red-500');
                } else {
                    logItem.classList.add('border-gray-500');
                }
                
                logItem.innerHTML = `<p>${entry.text}</p>`;
                logElement.appendChild(logItem);
            });
            
            // 滚动到底部
            logElement.scrollTop = logElement.scrollHeight;
        }
        
        // 添加日志条目
        function addLog(text, type = "normal") {
            gameData.log.push({ text, type });
            // 限制日志长度
            if (gameData.log.length > 20) {
                gameData.log.shift();
            }
        }
        
        // 静心冥想
        function meditate() {
            console.log("静心冥想")
            // 冥想主要提升悟性，可能增加少量修为
            const insightGain = Math.floor(Math.random() * 2) + 1;
            const cultGain = Math.floor(Math.random() * 10) + 5;
            
            gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
            attemptAttributeGain('insight', insightGain);
            
            addLog(`你静心冥想了一年，修为增加${cultGain}点。`, "normal");
            
            // 有几率触发特殊事件
            if (Math.random() < 0.3) {
                const event = eventSystem.getRandomEvent();
                showEvent(event);
            } else {
                // 增加年龄，检查是否寿终正寝
                gameData.age += 1;
                checkGameOver();
                updateUI();
            }
        }
        
        // 潜心修炼
        function cultivate() {
            // 修炼主要增加修为，可能提升灵力
            const baseGain = 20;
            // 修炼效率受悟性影响
            const insightFactor = 1 + (gameData.attributes.insight / 20);
            const cultGain = Math.floor(Math.random() * baseGain * insightFactor) + baseGain;
            
            gameData.cultivation = Math.min(gameData.cultivation + cultGain, gameData.maxCultivation);
            
            // 有几率提升灵力
            if (Math.random() < 0.4) {
                const spiritGain = Math.floor(Math.random() * 2) + 1;
                attemptAttributeGain('spirit', spiritGain);
            }
            
            addLog(`你潜心修炼了一年，修为增加${cultGain}点。`, "normal");
            
            // 有几率触发特殊事件
            if (Math.random() < 0.2) {
                const event = eventSystem.getRandomEvent();
                showEvent(event);
            } else {
                // 增加年龄，检查是否寿终正寝
                gameData.age += 1;
                checkGameOver();
                updateUI();
            }
        }
        
        // 外出历练
        function adventure() {
            addLog("你外出历练了一年。", "normal");
            
            // 历练主要提升根骨，事件概率高
            const event = eventSystem.getRandomEvent();
            showEvent(event);
        }
        
        // 显示突破弹窗
        function showBreakthroughModal() {
            // 检查是否可以突破（修为达到当前境界要求）
            if (gameData.cultivation < gameData.maxCultivation) {
                addLog("修为不足，无法突破境界。", "bad");
                updateUI();
                return;
            }
            
            // 计算突破成功率（受悟性和根骨影响）
            const baseRate = 50;
            const insightBonus = gameData.attributes.insight * 2;
            const constitutionBonus = gameData.attributes.constitution * 1;
            const successRate = Math.min(90, baseRate + insightBonus + constitutionBonus);
            
            // 更新弹窗信息
            document.getElementById('breakthrough-message').textContent = 
                `你准备突破到${realms[gameData.realm + 1].name}，成功率${successRate}%。突破过程中可能遭遇心魔侵袭！`;
            
            breakthroughModal.classList.remove('hidden');
        }
        
        // 隐藏突破弹窗
        function hideBreakthroughModal() {
            breakthroughModal.classList.add('hidden');
        }
        
        // 尝试突破境界
        function attemptBreakthrough() {
            hideBreakthroughModal();
            
            // 计算突破成功率
            const baseRate = 50;
            const insightBonus = gameData.attributes.insight * 2;
            const constitutionBonus = gameData.attributes.constitution * 1;
            const successRate = Math.min(90, baseRate + insightBonus + constitutionBonus);
            
            const success = Math.random() * 100 < successRate;
            
            if (success) {
                // 突破成功
                gameData.realm += 1;
                gameData.cultivation = 0;
                gameData.maxCultivation = realms[gameData.realm].required;
                gameData.lifespan = realms[gameData.realm].maxAge;
                
                // 突破成功提升所有属性
                for (const attr of Object.keys(gameData.attributes)) {
                    attemptAttributeGain(attr, 1);
                }
                
                addLog(`恭喜！你成功突破到${realms[gameData.realm].name}！寿元增加到${gameData.lifespan}年。`, "major");
                
                // 飞升成功
                if (gameData.realm >= realms.length - 1 && gameData.cultivation >= gameData.maxCultivation) {
                    showGameOver(true);
                    return;
                }
            } else {
                // 突破失败
                const cultLoss = Math.floor(gameData.cultivation * 0.3);
                gameData.cultivation = Math.max(0, gameData.cultivation - cultLoss);
                
                // 随机降低一个属性
                const attributes = Object.keys(gameData.attributes);
                const randomAttr = attributes[Math.floor(Math.random() * attributes.length)];
                attemptAttributeLoss(randomAttr, 1);
                
                addLog(`突破失败，修为减少${cultLoss}点，${getAttributeName(randomAttr)}下降了1点。`, "bad");
            }
            
            // 突破无论成功失败都消耗一年
            gameData.age += 1;
            checkGameOver();
            updateUI();
        }
        
        // 显示事件弹窗
        function showEvent(event) {
            eventTitle.textContent = event.name;
            eventDescription.textContent = event.description;
            
            // 处理选择型事件
            if (event.choices && event.choices.length > 0) {
                eventChoices.innerHTML = '';
                eventChoices.classList.remove('hidden');
                
                event.choices.forEach((choice, index) => {
                    const button = document.createElement('button');
                    button.className = 'px-4 py-2 bg-primary/20 hover:bg-primary/30 text-white rounded-lg transition-all';
                    button.textContent = choice.text;
                    button.addEventListener('click', () => {
                        choice.effect();
                        hideEventModal();
                        
                        // 事件处理完毕，增加年龄并检查游戏状态
                        gameData.age += 1;
                        checkGameOver();
                        updateUI();
                    });
                    
                    eventChoices.appendChild(button);
                });
            } else {
                eventChoices.classList.add('hidden');
                
                // 非选择型事件，在关闭弹窗时执行效果
                closeEventBtn.onclick = () => {
                    event.effect();
                    hideEventModal();
                    
                    // 事件处理完毕，增加年龄并检查游戏状态
                    gameData.age += 1;
                    checkGameOver();
                    updateUI();
                };
            }
            
            eventModal.classList.remove('hidden');
        }
        
        // 隐藏事件弹窗
        function hideEventModal() {
            eventModal.classList.add('hidden');
            // 重置关闭按钮事件
            closeEventBtn.onclick = hideEventModal;
        }
        
        // 检查游戏是否结束（寿终正寝）
        function checkGameOver() {
            if (gameData.age >= gameData.lifespan) {
                showGameOver(false);
            }
        }
        
        // 显示游戏结束弹窗
        function showGameOver(isSuccess) {
            if (isSuccess) {
                gameOverTitle.textContent = "恭喜飞升";
                gameOverMessage.textContent = `你成功渡过天劫，飞升成仙，成为一代仙尊！享年${gameData.age}岁。`;
                finalAchievement.textContent = "九天仙尊";
            } else {
                gameOverTitle.textContent = "寿终正寝";
                gameOverMessage.textContent = `你享年${gameData.age}岁，止步于${realms[gameData.realm].name}，未能得道成仙。`;
                
                // 根据最终境界设置成就
                let achievement = "";
                if (gameData.realm >= 24) achievement = "渡劫大能";
                else if (gameData.realm >= 21) achievement = "大乘真人";
                else if (gameData.realm >= 18) achievement = "合体修士";
                else if (gameData.realm >= 15) achievement = "炼虚散人";
                else if (gameData.realm >= 12) achievement = "化神长老";
                else if (gameData.realm >= 9) achievement = "元婴真君";
                else if (gameData.realm >= 6) achievement = "金丹大师";
                else if (gameData.realm >= 3) achievement = "筑基道长";
                else achievement = "炼气修士";
                
                finalAchievement.textContent = achievement;
            }
            
            gameOverModal.classList.remove('hidden');
        }
        
        // 初始化游戏
        function initGame() {
            initEventListeners();
        }
        
        // 启动游戏
        window.addEventListener('DOMContentLoaded', initGame);
    </script>
</body>
</html>